package com.atguigu.serurity.config;

import com.atguigu.serurity.filter.TokenAuthenticationFilter;
import com.atguigu.serurity.filter.TokenLoginFilter;
import com.atguigu.serurity.security.MyPasswordEncoder;
import com.atguigu.serurity.security.TokenLogoutHandler;
import com.atguigu.serurity.security.TokenManager;
import com.atguigu.serurity.security.UnauthorizedEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * Security配置类
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {
    private RedisTemplate redisTemplate;
    // 用户信息
    private UserDetailsService userDetailsService;
    // 密码编码器
    private MyPasswordEncoder myPasswordEncoder;
    // token管理工具
    private TokenManager tokenManager;

    @Autowired
    public TokenWebSecurityConfig(UserDetailsService userDetailsService, MyPasswordEncoder myPasswordEncoder,
                                  TokenManager tokenManager, RedisTemplate redisTemplate) {
        this.userDetailsService = userDetailsService;
        this.myPasswordEncoder = myPasswordEncoder;
        this.tokenManager = tokenManager;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 安全拦截机制
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                // 自定义没有访问权限处理类
                .authenticationEntryPoint(new UnauthorizedEntryPoint())
                .and().csrf().disable()// 屏蔽CSRF(跨站请求伪造)
                .authorizeRequests()// 允许基于使用HttpServletRequest限制访问
                .anyRequest().authenticated()// 认证后才可访问
                // 自定义退出路径
                .and().logout().logoutUrl("/admin/acl/index/logout")
                // 自定义退出处理类
                .addLogoutHandler(new TokenLogoutHandler(tokenManager, redisTemplate))
                .and()
                // 自定义认证过滤器类
                .addFilter(new TokenLoginFilter(authenticationManager(), tokenManager, redisTemplate))
                // 自定义授权过滤器类
                .addFilter(new TokenAuthenticationFilter(authenticationManager(), tokenManager, redisTemplate))
                .httpBasic();
    }

    /**
     * 密码编码器
     */
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 指定UserDetailsService实现类 获取数据库中用户信息
        // 指定PasswordEncoder实现类  指定密码加密方式
        auth.userDetailsService(userDetailsService).passwordEncoder(myPasswordEncoder);
    }

    /**
     * 配置哪些请求不拦截
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/api/**",
                "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**"
        );
    }
}